#include "StdAfx.h"
#include "ButtonProperty.h"
#include "ModelPropManager.h"

#define AFX_PROP_HAS_BUTTON 0x0002

/////////////////////////////////////////////////////////////////////////////
// CButtonProperty object

IMPLEMENT_DYNAMIC(CButtonProperty, CMFCPropertyGridProperty)

	CButtonProperty::CButtonProperty(const CString& strName, const CString& strFolderName, DWORD_PTR dwData, LPCTSTR lpszDescr) : CMFCPropertyGridProperty(strName, COleVariant((LPCTSTR)strFolderName), lpszDescr, dwData)
{
	m_dwFlags = AFX_PROP_HAS_BUTTON;
	m_pModelPropManager=NULL;
	m_bEnabled = true;
}

CButtonProperty::~CButtonProperty(void)
{
}

void CButtonProperty::OnClickButton(CPoint /*point*/)
{
	if (!m_bEnabled)
	{
		return;
	}
	ASSERT_VALID(this);
	ASSERT_VALID(m_pWndList);
	ASSERT_VALID(m_pWndInPlace);
	ASSERT(::IsWindow(m_pWndInPlace->GetSafeHwnd()));

	m_bButtonIsDown = TRUE;
	Redraw();

	CString strPath = m_varValue.bstrVal;
	BOOL bUpdate = TRUE;

	if (bUpdate)
	{
		if (m_pWndInPlace != NULL)
		{
			m_pWndInPlace->SetWindowText(strPath);
		}

		m_varValue = (LPCTSTR) strPath;
	}

    //AfxMessageBox(_T("aaaaa"));
	if(m_pModelPropManager)
	{
		CString strValue;
		if (m_pModelPropManager->ButtonPressed(GetData(), strValue))
		{
			SetValue(_bstr_t(strValue));
		}
	}

	//CString strCircuit=pDevice->GetCircuitFileString();
	//SetValue();

	m_bButtonIsDown = FALSE;
	Redraw();
	
	if (m_pWndInPlace != NULL)
	{
		m_pWndInPlace->SetFocus();
	}
	else
	{
		m_pWndList->SetFocus();
	}
}
